<template>
	<view class="container">
		<view class="bg">
			<view class="loginInfo">
				<view class="line" v-if="account">登录账号：{{account}}</view>
				<view class="line" v-else @click="loginout">请登录</view>
				<!-- <view class="line">房间号：18#1121</view> -->
			</view>
		</view>
		<view class="list">
			<!-- <view class="item" @click="goPwd">密码修改</view> -->
			<view class="item" @click="checkVersion">检查更新</view>
		</view>
		<!-- #ifdef APP -->
		<p style="text-align: center;margin-top: 30px;">当前版本：{{version}}</p>
		<!-- #endif -->
		<button type="primary" style="width: 90%;margin: 30px auto;background: #4c89ef;" @click="loginout" v-if="account">退出</button>
	</view>
</template>

<script>
	import {
		getAppVersion
	} from "@/api/home.js"
	export default {
		data() {
			return {
				account: uni.getStorageSync("username") || "",
				version: ""
			}
		},
		onShow() {},
		watch: {},
		onLoad() {
			plus.runtime.getProperty(plus.runtime.appid, function(inf) {
				this.version = inf.version;
				console.log("当前应用版本：" + this.version);
			});
		},
		methods: {
			loginout() {
				uni.clearStorageSync();
				uni.reLaunch({
					url: "/pages/mine/login"
				})
			},
			goPwd() {
				uni.navigateTo({
					url: "updatePwd"
				})
			},
			checkVersion() {
				var that = this;
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					that.version = inf.version;
					console.log("当前应用版本：" + that.version);
					that.checkUpdate();
				});
			},
			checkUpdate() {
				var that = this;
				getAppVersion({}).then(res => {
					if (res.code == 200) {
						var arr = res.rows;
						if (arr.length > 0) {
							var dict = arr[0];
							var newVer = dict.appVersion;
							console.log(newVer);
							console.log(that.version);
							if (that.version && newVer && (that.version != newVer) && (that.version < newVer)) {
								that.downWgt(dict.appPath); // 下载升级包
							} else {
								plus.nativeUI.alert("无新版本可更新！");
							}
						}
					}
				})
			},
			downWgt(newapk) {
				console.log(newapk);
				var that = this;
				plus.nativeUI.showWaiting("下载更新文件...");
				plus.downloader.createDownload(newapk, {}, function(d, status) {
					if (status == 200) {
						console.log("下载更新文件成功：" + d.filename);
						that.installWgt(d.filename); // 安装wgt包
					} else {
						console.log("下载更新文件失败！");
						plus.nativeUI.alert("下载更新文件失败！");
					}
					plus.nativeUI.closeWaiting();
				}).start();
			},
			installWgt(path) {
				plus.nativeUI.showWaiting("安装更新文件文件...");
				plus.runtime.install(path, {
					force: true
				}, function() {
					plus.nativeUI.closeWaiting();
					console.log("安装更新文件文件成功！");
					plus.nativeUI.alert("应用资源更新完成！", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装更新文件文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装更新文件文件失败[" + e.code + "]：" + e.message);
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bg {
		width: 100%;
		height: 220px;
		background-color: #4c89ef;
	}

	.loginInfo {
		margin-left: 60px;
		color: #fff;
		font-size: 20px;
		padding-top: 30px;

		.line {
			margin-top: 70px;
		}
	}

	.list {
		.item {
			border-bottom: 1px solid #ccc;
			height: 60px;
			line-height: 60px;
			text-indent: 30px;
		}
	}
</style>